package logic

import (
	"cart_srv/cart"
	"cart_srv/internal/svc"
	"cart_srv/model/mysql"
	"cart_srv/model/redis"
	"context"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddGoodsLogic {
	return &AddGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddGoodsLogic) AddGoods(in *cart.AddGoodsRequest) (*cart.AddGoodsResponse, error) {
	g := mysql.Good{
		GoodsName:  in.Goodsname,
		GoodsPrice: float64(in.Goodsprice),
		GoodsStock: int(in.Goodsstock),
		GoodsImg:   in.Goodsimg,
	}
	err := g.GoodsCreate()
	if err != nil {
		logx.Error("商品发布失败")
		return nil, errors.New("商品发布失败")
	}
	redis.SyncStock(int64(g.ID), in.Goodsstock)
	return &cart.AddGoodsResponse{GoodsId: int64(g.ID)}, nil
}
